//
//  FCAddCoinsVC.m
//  FundChain
//
//  Created by 肖浩然的mac on 2018/2/27.
//  Copyright © 2018年 RUIXIN. All rights reserved.
//

#import "FCAddCoinsVC.h"
#import "FCHomeVC.h"
#import "XLSlideMenu.h"
#import "FCBaseNavigationVC.h"
#import "FCLeftBarVC.h"
#import "FCAddCoinsCell.h"
#import "FCWalletsModel.h"
#import "FCBalanceModel.h"
#import "FCExchangeRatesModel.h"
#import "FCManageCoinsModel.h"
@interface FCAddCoinsVC ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    UIView     *_viewNav;
}
/* imageIcon */
@property (strong, nonatomic) NSArray *imageArr;
/* title */
@property (strong, nonatomic) NSArray *titleArr;
/* 余额model */
@property (strong, nonatomic) FCBalanceModel  *balanceModel;

/* collectionView */
@property (strong, nonatomic)  UICollectionView *collectionView;

/* CoinsData */
@property (strong, nonatomic) NSMutableArray *balanceArr;

/* 选择的个数 */
@property (strong, nonatomic) NSMutableArray *selectArr;
/* 未选择的个数 */
@property (strong, nonatomic) NSMutableArray *dselectArr;
/* 所有钱包 */
@property (strong, nonatomic) NSMutableArray *allWalletArr;

/*所有钱包信息*/
@property (strong, nonatomic) FCWalletsModel  *walletModel;

/* skipCoinsArr */
@property (strong, nonatomic) NSMutableArray *skipCoinsArr;
/* skipBalanceArr */
@property (strong, nonatomic) NSMutableArray *skipBalanceArr;
/* <#Annotation#> */
@property (strong, nonatomic) NSMutableArray *firstArr;
/* <#Annotation#> */
@property (strong, nonatomic) NSMutableArray *didClickArr;
/* <#Annotation#> */
/* <#Annotation#> */
@property (strong, nonatomic) UIButton *Btn;
@property (strong, nonatomic) NSMutableArray *arrMut;
@property (strong, nonatomic) FCWalletsModel *walletModel1;
@property (strong, nonatomic) FCWalletsModel *walletModel2;
@property (strong, nonatomic) FCWalletsModel *walletModel3;
@property (strong, nonatomic) FCWalletsModel *walletModel4;
@property (strong, nonatomic) FCWalletsModel *walletModel5;

@property (copy, nonatomic) NSString *cchStatus;
@end

@implementation FCAddCoinsVC


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
        [self getCCHStatus];
    
   
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //    self.showBackItem = YES;
    [FCLocalUserHelper saveIsMainVC:@"0"];
    [self getdidClickAllMoneyType];
    [self getFirstAllMoneyType];
    
    //货币icon
    [self addImageArr];
    [self addTitleArr];
    [self setupUI];
    [self setNav];
    
    
}
#pragma mark-设置导航栏
-(void)setNav{
    self.navigationController.navigationBarHidden = YES;
    _viewNav =[UIView new];
    [self.view addSubview:_viewNav];
    [_viewNav mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                
                make.left.right.top.mas_equalTo(self.view.safeAreaInsets);
                make.height.mas_equalTo(88);
            }else{
                make.left.right.mas_equalTo(0);
                make.height.mas_equalTo(64);
                make.top.mas_equalTo(0);
            }
        }else{
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(64);
            make.top.mas_equalTo(0);
        }
        
    }];
    _viewNav.backgroundColor = [UIColor clearColor];
    
    UIImageView *im = [UIImageView new];
    [_viewNav addSubview:im];
    [im mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    im.image = [UIImage imageNamed:@"bg_login_top"];
    im.userInteractionEnabled = YES;
    UIButton *leftBtn = [UIButton new];
    [self.view addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15*KScreenWidthScale750);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    [leftBtn setImage:[UIImage imageNamed:@"icon_back_black"]  forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    //事件
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)leftBtnClick{
    [self.firstArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        FCWalletsModel *model = obj;
        [self postCoinsTypeWith:model.walletId :YES];
        
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self  pushSkipMainVC];
    });
}
-(void)addImageArr{
    
    self.imageArr = @[@"btc_50pt",@"ltc_50pt",@"bch_50pt",@"cch_50pt",@"zec_50pt"];
}
-(void)addTitleArr{
    
    self.titleArr = @[Localized(@"比特币"),Localized(@"莱特币"),Localized(@"比特币现金"),Localized(@"加密现金币"),Localized(@"零币")];
}
#pragma mark-按钮事件
//跳过
-(void)skipBtnClick{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self.firstArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            FCWalletsModel *model = obj;
            [self postCoinsTypeWith:model.walletId :YES];
            
        }];
        
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
        [self  pushSkipMainVC];
    });
}
#pragma mark-添加货币种类

-(void)completeBtnClick{
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [FCLocalUserHelper pushMainTabbarController];
    });
    
}
-(void)setupUI{
    //背景图片
    UIImageView *bgImageView = [UIImageView new];
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(64);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    bgImageView.image = [UIImage imageNamed:@"bg_addcoins"];
    //登陆label
    UILabel *loginLabel = [UILabel new];
    [self.view addSubview:loginLabel];
    [loginLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                make.left.mas_equalTo(32*KScreenWidthScale750);
                make.top.mas_equalTo((100+128)*KScreenHeightScale1334);
                
            }else{
                make.left.mas_equalTo(32*KScreenWidthScale750);
                make.top.mas_equalTo(228*KScreenHeightScale1334);
                
            }
        }else{
            make.left.mas_equalTo(32*KScreenWidthScale750);
            make.top.mas_equalTo(228*KScreenHeightScale1334);
            
        }
    }];
    loginLabel.text = Localized(@"添加货币");
    loginLabel.font = KOverstrikingfont(52);
    //提示语
    UILabel *hintLabel = [UILabel new];
    [self.view addSubview:hintLabel];
    [hintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(loginLabel);
        make.top.mas_equalTo(loginLabel.mas_bottom).offset(30*KScreenHeightScale1334);
        make.right.mas_equalTo(-55*KScreenWidthScale750);
    }];
    hintLabel.font = Kfont(28);
    hintLabel.textColor = kGrayColor;
    hintLabel.text = Localized(@"选择你要添加到钱包的货币。 您也可以跳过并稍后添加。");
    hintLabel.numberOfLines = 2;
    
    //设置瀑布流
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setScrollDirection:(UICollectionViewScrollDirectionVertical)];
    //    layout.minimumLineSpacing = 10=;// 水平方向的间距
    //    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(0, 15, 10, 15);
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView = collectionView;
    [self.view addSubview:collectionView];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(hintLabel.mas_bottom).offset(80*KScreenHeightScale1334);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.backgroundColor = [UIColor clearColor];
    [collectionView registerClass:[FCAddCoinsCell class] forCellWithReuseIdentifier:@"addCoinsCollectionViewCell"];
    //设置可以多选
    collectionView.allowsMultipleSelection=YES;
    //底部View
    UIView *bottomView = [UIView new];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(96*KScreenHeightScale1334);
    }];
    bottomView.backgroundColor = kWhiteColor;
    //skipBtn
    UIButton *skipBtn = [UIButton new];
    [bottomView addSubview:skipBtn];
    [skipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH/2-0.5);
        make.top.bottom.mas_equalTo(0);
    }];
    [skipBtn setTitle:Localized(@"跳过") forState:UIControlStateNormal];
    [skipBtn setTitleColor:kDarkBlackColor forState:UIControlStateNormal];
    skipBtn.titleLabel.font = Kfont(36);
    //分割线
    UIView *viewLine =[UIView new];
    [bottomView addSubview:viewLine];
    [viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(skipBtn.mas_right);
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(56*KScreenHeightScale1334);
        make.width.mas_equalTo(1);
    }];
    viewLine.backgroundColor =kLineColor;
    //完成按钮
    UIButton *completeBtn = [UIButton new];
    [bottomView addSubview:completeBtn];
    [completeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewLine);
        make.right.mas_equalTo(0);
        make.top.bottom.mas_equalTo(0);
    }];
    [completeBtn setTitle:Localized(@"完成") forState:UIControlStateNormal];
    [completeBtn setTitleColor:kRedOrangeColor forState:UIControlStateNormal];
    completeBtn.titleLabel.font = KOverstrikingfont(36);
    
    //按钮事件
    [skipBtn addTarget:self action:@selector(skipBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [completeBtn addTarget:self action:@selector(completeBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark-UIcollectionView&UICollectionViewDataSource

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.allWalletArr.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (SCREEN_WIDTH == 320) {
        
        return CGSizeMake(320*KScreenWidthScale750, 300*KScreenHeightScale1334);
    }else{
        return CGSizeMake(320*KScreenWidthScale750, 292*KScreenHeightScale1334);
    }
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FCAddCoinsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"addCoinsCollectionViewCell" forIndexPath:indexPath];
    
    
    FCWalletsModel *model = self.allWalletArr[indexPath.item];
    
    cell.iconImageView.image = [UIImage imageNamed:model.iconImage];
    cell.nickNameLabel.text = model.walletName;
    if ([model.choice isEqualToString:@"1"]) {
        
        [cell.imageView setImage:[UIImage imageNamed:@"chosen_coin"] forState:UIControlStateNormal];
    }else{
        [cell.imageView setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    }
    cell.imageView.tag = 1000+indexPath.item;
    [self.balanceArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        FCBalanceModel *balanceModel = obj;
        
        if ([model.walletId isEqualToString:balanceModel.walletId]) {
            
            cell.distanceLabel.text = [NSString stringWithFormat:@"=%.2f %@",[balanceModel.balance floatValue],balanceModel.currentName];
            
            switch ([model.walletId integerValue]) {
                case 1001:
                {
                    cell.distanceLabel.text = [NSString stringWithFormat:@"≈%.2f USD",[balanceModel.balance floatValue]];
                }
                    break;
                case 1005:
                {
                    cell.distanceLabel.text = [NSString stringWithFormat:@"≈%.2f USD",[balanceModel.balance floatValue]];
                }
                    break;
                case 1006:
                {
                    cell.distanceLabel.text = [NSString stringWithFormat:@"≈%.2f USD",[balanceModel.balance floatValue]];
                }
                    break;
                case 1009:
                {
                    cell.distanceLabel.text = [NSString stringWithFormat:@"=%.8f CCH",[balanceModel.balance floatValue]];
                }
                    break;
                case 1010:
                {
                    cell.distanceLabel.text = [NSString stringWithFormat:@"≈%.2f USD",[balanceModel.balance floatValue]];
                }
                    break;
                    
                default:
                    break;
            }
        }
    }];
    
    
    
    
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
        
        
        FCAddCoinsCell *item = (FCAddCoinsCell*)[collectionView cellForItemAtIndexPath:indexPath];
        NSLog(@"点击了%@",item.nickNameLabel.text);
        FCWalletsModel *model = self.allWalletArr[indexPath.row];
    
    if ([self.cchStatus isEqualToString:@"0"]) {
        if (self.selectArr.count == 2) {
            UIButton *btn = [item viewWithTag:1000+indexPath.item];
            [btn setImage:[UIImage imageNamed:@"chosen_coin"] forState:UIControlStateNormal];
            btn.hidden = NO;
            [self showAlert:Localized(@"至少选择一个货币")];
            return;
        }else{
            UIButton *btn = [item viewWithTag:1000+indexPath.item];
            [btn setImage:nil forState:UIControlStateNormal];
            [self deleteCoin:model.walletId :NO];
            [self.selectArr removeObject:model.walletId];
        }
    }else{
        if (self.selectArr.count == 1) {
            UIButton *btn = [item viewWithTag:1000+indexPath.item];
            [btn setImage:[UIImage imageNamed:@"chosen_coin"] forState:UIControlStateNormal];
            btn.hidden = NO;
            [self showAlert:Localized(@"至少选择一个货币")];
            return;
        }else{
            UIButton *btn = [item viewWithTag:1000+indexPath.item];
            [btn setImage:nil forState:UIControlStateNormal];
            [self deleteCoin:model.walletId :NO];
            [self.selectArr removeObject:model.walletId];
        }
    }
    
   
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.cchStatus isEqualToString:@"0"]) {
        if (self.selectArr.count == 2) {
            [self showAlert:Localized(@"至少选择一个货币")];
            
        }
    }else{
        if (self.selectArr.count == 1) {
            [self showAlert:Localized(@"至少选择一个货币")];
            
        }
    }
    
        FCAddCoinsCell *item = (FCAddCoinsCell*)[collectionView cellForItemAtIndexPath:indexPath];
        NSLog(@"点击了%@",item.nickNameLabel.text);
        
        FCWalletsModel *model = self.allWalletArr[indexPath.row];
        UIButton *btn = [item viewWithTag:1000+indexPath.item];
        [btn setImage:[UIImage imageNamed:@"chosen_coin"] forState:UIControlStateNormal];
        [self.selectArr addObject:model.walletId];
        [self postCoinsTypeWith:model.walletId];
//    if (self.selectArr.count == 1) {
//        UIButton *btn = [item viewWithTag:1000+indexPath.item];
//        [btn setImage:[UIImage imageNamed:@"chosen_coin"] forState:UIControlStateNormal];
//        btn.hidden = NO;
//        [self showAlert:Localized(@"至少选择一个货币")];
//        return;
//    }
}


#pragma mark-上传货币种类
-(void)postCoinsTypeWith:(NSString*)walletId{
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *arrM =[FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",arrM[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",arrM[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    [dict setObject:[FCLocalUserHelper getXidStr] forKey:@"XID"];
    [dict setObject:walletId forKey:@"walletId"];
    NSString *url = Add_wallet_type_URL;
    
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                
                NSLog(@"%@",responseObject);
                [FCLocalUserHelper saveLoginStates:@"1"];
                [self getdidClickAllMoneyType];
            }
            
            
        }
        
        
        
    } faile:^(NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        }
        
    }];
    
}
-(void)postCoinsTypeWith:(NSString*)walletId :(BOOL)isSkip{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *arrM =[FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",arrM[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",arrM[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    [dict setObject:[FCLocalUserHelper getXidStr] forKey:@"XID"];
    [dict setObject:walletId forKey:@"walletId"];
    NSString *url = Add_wallet_type_URL;
    __weak __typeof(self)wself = self;
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                
                NSLog(@"%@",responseObject);
                [FCLocalUserHelper saveLoginStates:@"1"];
                if (isSkip) {
                    
                }else{
                    
                        [self getAllWalletMessage];
                        
                   
                    
                }
            }
            
            
        }
        
        
        
    } faile:^(NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        }
        
    }];
    
    
}


-(void)pushSkipMainVC{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    FCHomeVC *VC = [FCHomeVC new];
    VC.coinsArr = self.skipCoinsArr;
    VC.balanceArr = self.skipBalanceArr;
    VC.isSkipAddCoins = YES;
    FCBaseNavigationVC *nav = [[FCBaseNavigationVC alloc]initWithRootViewController:VC];
    XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:nav];
    slideMenu.leftViewController =[[FCLeftBarVC alloc]init];
    window.rootViewController = slideMenu;
    [window makeKeyAndVisible];
    
}

#pragma mark-获取货币余额
-(void)getCoinsBalance{
    
    if (!kArrayIsEmpty(self.allWalletArr)) {
        
        NSArray *walletIdArr = self.allWalletArr;
        
        [walletIdArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            FCWalletsModel *model = obj;
            [self postBalanceDataWith:model.walletId];
            
        }];
    }
}
-(void)postBalanceDataWith:(NSString*)index{
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *arrM =[FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",arrM[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",arrM[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    [dict setObject:[FCLocalUserHelper getXidStr] forKey:@"XID"];
    [dict setObject:index forKey:@"walletId"];
    NSString *url = Get_balance_URL;
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        
        if (responseObject) {
            NSLog(@"%@",responseObject);
            if ([responseObject[@"code"]integerValue] == 200) {
                
                _balanceModel = [FCBalanceModel mj_objectWithKeyValues:responseObject[@"result"]];
                NSString *xidStr = [FCLocalUserHelper getXidStr];
                _balanceModel.XID = xidStr;
                //获取每个货币的列表数据
                _balanceModel.walletId = index;
                
                NSLog(@"walid%@",index);
                
                [self getCurrentResultDataWith:xidStr];
                
                
            }
            
            
        }
        
        
    } faile:^(NSError *error) {
        
        if (error) {
            NSLog(@"%@",error);
        }
    }];
    
    
}
#pragma mark-获取每个货币的列表数据
-(void)getCurrentResultDataWith:(NSString*)xidStr{
    
    NSArray *currentResult = _balanceModel.currentResult;
    _balanceModel.currentResultArr = [NSMutableArray array];
    
    [currentResult enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%@",obj);
        FCCurrentResultModel *model = [FCCurrentResultModel mj_objectWithKeyValues:obj];
        model.walletId = _balanceModel.walletId;
        model.XID = xidStr;
        _balanceModel.amt = model.amt;
        [_balanceModel.currentResultArr addObject:model];
        
    }];
    if ([_balanceModel.walletId isEqualToString:@"1009"]) {
        [self.skipBalanceArr addObject:_balanceModel];
    }
    if ([_balanceModel.walletId isEqualToString:@"1001"]) {
        [self.skipBalanceArr addObject:_balanceModel];
    }
    
    [self.balanceArr addObject:_balanceModel];
    
    
    
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
    });
    
    
}
#pragma mark-获取所有钱包信息
-(void)getAllWalletMessage{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *signArr = [FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",signArr[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",signArr[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    [dict setObject:[FCLocalUserHelper getXidStr] forKey:@"XID"];
    NSString *url = Get_load_wallets_URL;
    __weak __typeof(self)wself = self;
    [self showChrysanthemumAlert:nil];
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            NSLog(@"%@",responseObject);
            if ([responseObject[@"code"] integerValue] == 200) {
                NSArray *arr = responseObject[@"result"];
                if (self.allWalletArr.count > 0) {
                    [self.allWalletArr removeAllObjects];
                }
                [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    FCWalletsModel *model = [FCWalletsModel mj_objectWithKeyValues:obj];
                    NSString *xidStr = [FCLocalUserHelper getXidStr];
                    model.XID = xidStr;
                    switch ([model.walletId integerValue]) {
                        case 1001:
                        {
                            model.iconImage = self.imageArr[0];
                            model.walletName = self.titleArr[0];
                        }
                            break;
                        case 1005:
                        {
                            model.iconImage = self.imageArr[1];
                            model.walletName = self.titleArr[1];
                        }
                            break;
                        case 1006:
                        {
                            model.iconImage = self.imageArr[2];
                            model.walletName = self.titleArr[2];
                        }
                            break;
                        case 1009:
                        {
                            model.iconImage = self.imageArr[3];
                            model.walletName = self.titleArr[3];
                        }
                            break;
                        case 1010:
                        {
                            model.iconImage = self.imageArr[4];
                            model.walletName = self.titleArr[4];
                        }
                            break;
                        default:
                            break;
                            
                            
                    }
                    
                    if ([self.cchStatus isEqualToString:@"0"]) {
                        
                       
                        if ([model.walletId isEqualToString:@"1001"]) {
                            self.walletModel2 = model;
                            [wself.arrMut addObject:self.walletModel2];
                            [self.skipCoinsArr addObject:model];
                        }
                        if ([model.walletId isEqualToString:@"1006"]) {
                            self.walletModel3 = model;
                            [wself.arrMut addObject:self.walletModel3];
                        }
                        if ([model.walletId isEqualToString:@"1005"]) {
                            self.walletModel4 = model;
                            [wself.arrMut addObject:self.walletModel4];
                        }
                        if ([model.walletId isEqualToString:@"1010"]) {
                            self.walletModel5 = model;
                            [wself.arrMut addObject:self.walletModel5];
                        }
                        if ([model.choice isEqualToString:@"1"]) {
                            
                            [self.selectArr addObject:model.walletId];
                        }
                       
                    }else{
                        if ([model.walletId isEqualToString:@"1009"]) {
                            self.walletModel1 = model;
                            [wself.arrMut addObject:self.walletModel1];
                            [self.skipCoinsArr addObject:model];
                        }
                        if ([model.walletId isEqualToString:@"1001"]) {
                            self.walletModel2 = model;
                            [wself.arrMut addObject:self.walletModel2];
                            [self.skipCoinsArr addObject:model];
                        }
                        if ([model.walletId isEqualToString:@"1006"]) {
                            self.walletModel3 = model;
                            [wself.arrMut addObject:self.walletModel3];
                        }
                        if ([model.walletId isEqualToString:@"1005"]) {
                            self.walletModel4 = model;
                            [wself.arrMut addObject:self.walletModel4];
                        }
                        if ([model.walletId isEqualToString:@"1010"]) {
                            self.walletModel5 = model;
                            [wself.arrMut addObject:self.walletModel5];
                        }
                        if ([model.choice isEqualToString:@"1"]) {
                            
                            [self.selectArr addObject:model.walletId];
                        }
                    }
                    
                    
                    
                }];
            
                wself.arrMut = [wself getAnArrayWithArray:self.arrMut];
                wself.allWalletArr =  wself.arrMut;
                //                 [self.collectionView reloadData];
                [self hideProgress]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    //获取货币余额
                    [self getCoinsBalance];
                    
                    [FCLocalUserHelper saveFMDBWalletsModelData:self.allWalletArr];
                });
            }
            
        }
        
    } faile:^(NSError *error) {
        if (error ) {
            NSLog(@"%@",error);
        }
        
    }];
    
    
    
    
}
-(void)deleteCoin:(NSString*)walletId: (BOOL)isSlect{
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *signArr = [FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",signArr[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",signArr[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    [dict setObject:[FCLocalUserHelper getXidStr] forKey:@"XID"];
    [dict setObject:walletId forKey:@"walletId"];
    NSString *url = Remove_wallet_type_URL;
    
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        
        if (responseObject) {
            NSLog(@"%@",responseObject);
            if ([responseObject[@"code"] integerValue] == 200) {
                
                //获取所有钱包信息
                
                //发送移除的通知
                [[NSNotificationCenter defaultCenter]postNotificationName:@"removeCoins" object:nil];
                [self hideProgress];
                if (isSlect) {
                    
                }else{
                    [self getdidClickAllMoneyType];
                    
                    
                }
            }else if ([responseObject[@"code"] integerValue] == 400){
                
                [self showAlert:Localized(@"至少选择一个货币")];
                
                
            }
        }
        
    } faile:^(NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        }
        
    }];
}

-(void)deleteCoins:(NSString*)walletId: (BOOL)isSlect{
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *signArr = [FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",signArr[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",signArr[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    [dict setObject:[FCLocalUserHelper getXidStr] forKey:@"XID"];
    [dict setObject:walletId forKey:@"walletId"];
    NSString *url = Remove_wallet_type_URL;
    
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        
        if (responseObject) {
            NSLog(@"%@",responseObject);
            if ([responseObject[@"code"] integerValue] == 200) {
                
                //获取所有钱包信息
                
                //发送移除的通知
                [[NSNotificationCenter defaultCenter]postNotificationName:@"removeCoins" object:nil];
                [self hideProgress];
                if (isSlect) {
                    
                }else{
                    
                    [self getAllWalletMessage];
                    
                    
                }
            }else if ([responseObject[@"code"] integerValue] == 400){
//                [self showAlert:Localized(@"至少选择一个货币")];
            }
        }
        
    } faile:^(NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        }
        
    }];
}
-(NSMutableArray *) getAnArrayWithArray:(NSMutableArray*)theArray
{
    NSMutableArray * mutableArray = [[NSMutableArray alloc]init];
    
    
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j<theArray.count; j++) {
            FCWalletsModel * modelj = theArray[j];
            if ([modelj.walletId isEqualToString:@"1009"] && i==0) {
                [mutableArray addObject:modelj];
                
            }
            if ([modelj.walletId isEqualToString:@"1001"] && i==1) {
                [mutableArray addObject:modelj];
            }
            if ([modelj.walletId isEqualToString:@"1006"] && i==2) {
                [mutableArray addObject:modelj];
            }
            if ([modelj.walletId isEqualToString:@"1005"] && i==3) {
                [mutableArray addObject:modelj];
            }
            if ([modelj.walletId isEqualToString:@"1010"] && i==4) {
                [mutableArray addObject:modelj];
            }
        }
    }
    return mutableArray;
}
-(void)getFirstAllMoneyType{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *signArr = [FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",signArr[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",signArr[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    [dict setObject:[FCLocalUserHelper getXidStr] forKey:@"XID"];
    NSString *url = Get_load_wallets_URL;
    
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                NSArray *arr = responseObject[@"result"];
                NSMutableArray *arrM = [NSMutableArray new];
                [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    FCWalletsModel *model = [FCWalletsModel mj_objectWithKeyValues:obj];
                    
                    if ([model.choice isEqualToString:@"1"]) {
                        
                        if ([self.cchStatus isEqualToString:@"0"]) {
                            if ([model.walletId isEqualToString:@"1009"]) {
                                
                            }else{
                                [arrM addObject:model];
                            }
                        }else{
                            [arrM addObject:model];
                        }
                        
                    }
                    
                }];
                self.firstArr = arrM;
            }
        }
        
        
    } faile:^(NSError *error) {
        
        
    }];
}
-(void)getdidClickAllMoneyType{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *signArr = [FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",signArr[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",signArr[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    [dict setObject:[FCLocalUserHelper getXidStr] forKey:@"XID"];
    NSString *url = Get_load_wallets_URL;
    
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                NSArray *arr = responseObject[@"result"];
                
                NSMutableArray *arrM = [NSMutableArray new];
                [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    FCWalletsModel *model = [FCWalletsModel mj_objectWithKeyValues:obj];
                    if ([model.choice isEqualToString:@"1"]) {
                        if ([self.cchStatus isEqualToString:@"0"]) {
                            if ([model.walletId isEqualToString:@"1009"]) {
                                
                            }else{
                                [arrM addObject:model];
                            }
                        }else{
                            [arrM addObject:model];
                        }
                        
                    }
                    
                }];
                self.didClickArr = arrM;
                
            }
        }
        
        
    } faile:^(NSError *error) {
        
        
    }];
}
-(void)getCCHStatus{
    
    NSString *url = @"https://app.fund-chain.io/Fundchain/index.php/Api/Base/is_show_coin_1_0";
    [FCSharedNetworkTool POSTWithURLString:url parameters:nil success:^(id responseObject) {
        if (responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                NSLog(@"CCH是%@",responseObject[@"result"][@"show"]);
                self.cchStatus = [NSString stringWithFormat:@"%@",responseObject[@"result"][@"show"]];
                NSString *res = [NSString stringWithFormat:@"%@",responseObject[@"result"][@"show"]];
                 [self getAllWalletMessage];
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    FCCCHStatusModel *model = [FCCCHStatusModel new];
                    model.statusStr = res;
                    [FCLocalUserHelper saveCCHStatus:model];
                });
                
                
            }
        }
        
        
    } faile:^(NSError *error) {
        
        
    }];
}
#pragma mark-懒加载
-(NSMutableArray *)firstArr{
    if (!_firstArr) {
        _firstArr = [NSMutableArray new];
        
    }
    return _firstArr;
}
-(NSMutableArray *)didClickArr{
    if (!_didClickArr) {
        _didClickArr = [NSMutableArray new];
        
    }
    return _didClickArr;
}
-(NSMutableArray *)balanceArr{
    if (!_balanceArr) {
        _balanceArr = [NSMutableArray new];
        
    }
    return _balanceArr;
}

-(NSMutableArray *)selectArr{
    if (!_selectArr) {
        _selectArr = [NSMutableArray new];
        
    }
    return _selectArr;
}
-(NSMutableArray *)allWalletArr{
    if (!_allWalletArr) {
        _allWalletArr = [NSMutableArray new];
        
    }
    return _allWalletArr;
}
-(NSMutableArray *)arrMut{
    if (!_arrMut) {
        _arrMut = [NSMutableArray new];
    }
    return _arrMut;
}
-(NSMutableArray *)skipCoinsArr{
    if (!_skipCoinsArr) {
        _skipCoinsArr = [NSMutableArray new];
    }
    return _skipCoinsArr;
}
-(NSMutableArray *)skipBalanceArr{
    if (!_skipBalanceArr) {
        _skipBalanceArr = [NSMutableArray new];
    }
    return _skipBalanceArr;
}
-(NSMutableArray *)dselectArr{
    if (!_dselectArr) {
        _dselectArr = [NSMutableArray new];
    }
    return _dselectArr;
}
@end
